* Figure 10a, and the Data generating process for Figure 10b

net install polychoric, from(https://staskolenikov.net/stata)

use MeasureComparisons.dta, clear

sort ccode year
gen MA = .
gen MP = .

set more off
forvalues x = 1/1000 { 
qui gen temp1 = rnormal(measure5, measure5_se)

qui polychoric temp1 aclpdemoc
sort ccode year
qui replace MA = r(rho) in `x'


qui polychoric temp1 polity2
sort ccode year
qui replace MP = r(rho) in `x'

* record the correlations
qui drop temp1
di "."
}

sum MA MP

centile MA MP


forvalues me = 50(1)99 { 
di "`me'"
local mem = `me'/100
qui gen MAdum_`me' = .

forval po = 140(10)180{
local pop = (`po'-90)/10

qui gen MPdum_`me'_`po' = .

qui gen poldum = .
qui replace poldum = 1 if polity2 >= `pop' & polity2!=. 
qui replace poldum = 0 if polity2 < `pop'  & polity2!=.


forvalues x = 1/500 { 

qui gen temp1 = rnormal(measure5, measure5_se)
qui gen mesdum = .
qui replace mesdum = 1 if temp1 >= `mem' & temp1!=. 
qui replace mesdum = 0 if temp1 < `mem'  & temp1!=.

qui tetrachoric mesdum aclpdemoc
sort ccode year
qui replace MAdum_`me' = r(rho) in `x'

qui tetrachoric mesdum poldum
sort ccode year
qui replace MPdum_`me'_`po' = r(rho) in `x'

qui drop temp1 mesdum
}
qui drop poldum
}

}



qui gen Pocut = .
qui gen Mecut = .
qui gen MP_05 = .
qui gen MP_50 = .
qui gen MP_95 = .

qui gen MA_05 = .
qui gen MA_50 = .
qui gen MA_95 = .

local alo = 1
forvalues me = 50(1)99 { 
forval po = 140(10)180{

qui replace Pocut = `po' in `alo'
qui replace Mecut = `me' in `alo'

qui centile MPdum_`me'_`po', centile(5 50 95)
qui sort ccode year
qui replace MP_05 = r(c_1) in `alo'
qui replace MP_50 = r(c_2) in `alo'
qui replace MP_95 = r(c_3) in `alo'

qui centile MAdum_`me', centile(5 50 95)
qui sort ccode year
qui replace MA_05 = r(c_1) in `alo'
qui replace MA_50 = r(c_2) in `alo'
qui replace MA_95 = r(c_3) in `alo'

local alo = `alo' + 1
}
}


gen Mecut2 = Mecut/100

twoway (line MA_50 Mecut2, color(black)) (line MA_05 Mecut2,  lp(dash)color(black)) (line MA_95 Mecut2,  lp(dash) color(black)), leg(off) xtitle("Cutpoint Choices for Binary Democracy") ytitle("Tetrachoric Correlation with ACLP")